package cn.david.product.service.impl;

import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.product.domain.Product;
import cn.david.product.domain.ProductDetail;
import cn.david.product.mapper.ProductDetailMapper;
import cn.david.product.mapper.ProductMapper;
import cn.david.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl extends BasicServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public ProductDetail findDetailByProductId(Long productId) {
        return productDetailMapper.loadByProductId(productId);
    }

    @Override
    public void onSale(Long[] ids) {
        productMapper.onSale(ids);
    }

    @Override
    public void fnSale(Long[] ids) {
        productMapper.fnSale(ids);

    }

    @Override
    public void add(Product product) {

        productMapper.save(product);//需要返回主键
        ProductDetail detail = product.getDetail();
        if(detail != null){
            detail.setProduct_id(product.getId());
            productDetailMapper.save(detail);
        }
    }

    @Override
    public void del(Long productId) {
        //1.通过服务Id 删除服务详情
        productDetailMapper.deleteByProductId(productId);
        productMapper.remove(productId);
    }

    @Override
    public void upadte(Product product) {
        productMapper.update(product);
        ProductDetail detail = product.getDetail();
        if(detail != null){
            detail.setProduct_id(product.getId());
            if(detail.getId() != null){
                productDetailMapper.update(detail);
            }else{
                productDetailMapper.save(detail);
            }

        }
    }
}
